<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>User Authentication</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100vh;
        }

        #container {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        input {
            width: 100%;
            padding: 10px;
            margin-bottom: 10px;
            box-sizing: border-box;
        }

        button {
            padding: 10px;
            background-color: #4caf50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        button.logout {
            background-color: #ff3333;
        }
    </style>
</head>
<body>
    <div id="container">
        <h2>User Authentication</h2>
        
        <div id="registerForm">
            <h3>Register</h3>
            <input type="text" id="regUsername" placeholder="Username" required>
            <input type="email" id="regEmail" placeholder="Email" required>
            <input type="password" id="regPassword" placeholder="Password" required>
            <button onclick="register()">Register</button>
        </div>

        <div id="loginForm">
            <h3>Login</h3>
            <input type="text" id="loginUsername" placeholder="Username" required>
            <input type="password" id="loginPassword" placeholder="Password" required>
            <button onclick="login()">Login</button>
        </div>

        <button class="logout" onclick="logout()">Logout</button>
        <p id="message"></p>
        <p id="loginStatus"></p>
    </div>

    <script>
        async function register() {
            const regUsername = document.getElementById('regUsername').value;
            const regEmail = document.getElementById('regEmail').value;
            const regPassword = document.getElementById('regPassword').value;

            const response = await fetch('http://127.0.0.1:5000/register', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ username: regUsername, email: regEmail, password: regPassword })
            });

            const result = await response.json();
            document.getElementById('message').innerText = result.message;
        }

        async function login() {
            const loginUsername = document.getElementById('loginUsername').value;
            const loginPassword = document.getElementById('loginPassword').value;

            const response = await fetch('http://127.0.0.1:5000/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ username: loginUsername, password: loginPassword })
            });

            const result = await response.json();
            document.getElementById('message').innerText = result.message;

            // Display login status
            if (result.message === 'Login successful!') {
                document.getElementById('loginStatus').innerText = 'Logged in as ' + loginUsername;
            } else {
                document.getElementById('loginStatus').innerText = '';
            }
        }

        async function logout() {
            const response = await fetch('http://127.0.0.1:5000/logout');
            const result = await response.json();
            document.getElementById('message').innerText = result.message;

            // Clear login status
            document.getElementById('loginStatus').innerText = '';
        }
    </script>
</body>
</html>
